package com.vlong.town.controller.admin.experience;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.experience.Experience;
import com.vlong.town.service.experience.ExperienceService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 旅游资讯
 * @author Assassin
 */
@Controller
@RequestMapping("/experience")
public class ExperienceController extends BaseController  {

	@Autowired
	private ExperienceService experienceService;// 业务接口

   /**
	* 进入
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return experience/add_experience
	*/
	@RequestMapping(value="/list")
	public String gotoExperienceListPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="title") String title,
			@RequestParam(required=false, value="desc") String desc,
			@RequestParam(required=false, value="content") String content
			) {
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
		List<Experience> experienceList = this.experienceService.queryList(null, null, 1, null, title, content, null, desc, null, null);
		PageInfo<Experience> pageInfo = new PageInfo<Experience>(experienceList);
        PageTag<Experience> pt = new PageTag<Experience>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(Experience experience : experienceList) {
        	experience.setContent(DataUtil.delHTMLTag(experience.getContent(), 0));
        }
        request.setAttribute("experienceList", experienceList);
		return "admin/experience/list_experience";
	}
   /**
	* 进入
	* @return experience/add_experience
	*/
	@RequestMapping(value="/gotoAddPage", method=RequestMethod.GET)
	public String gotoExperienceAddPage() {
		return "admin/experience/add_experience";
	}

	 /**
     * 添加旅游资讯
     * @param title 旅游资讯标题
     * @param content 旅游资讯内容
     * @param desc 旅游资讯简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/executeAddExperience")
    public DataMessage executeAddExperience(
            @RequestParam(name="title", required=false) String title,
            @RequestParam(name="content", required=false) String content,
            @RequestParam(name="desc", required=false) String desc,
            @RequestParam(name="picture", required=false) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("旅游资讯标题不合法");
        }else{
        	experienceService.add(1L,title, content, desc, picture);
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        	dm.setMessage("旅游资讯添加成功");
        }
        return dm;
    }

   /**
	* 进入
	* @param experienceId 主键值
	* @return experience/edit_experience
	*/
	@RequestMapping(value="/gotoEditPage/{experienceId}")
	public String gotoExperienceEditPage(@PathVariable(required=true, value="experienceId") Long experienceId) {
		Experience experience = this.experienceService.queryById(experienceId);
		request.setAttribute("experience", experience);
		request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + experience.getThumbnail());
		return "admin/experience/edit_experience";
	}

   /**
	* 编辑
	* 这里的参数需要根据实际情况自行修改
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/executeEditExperience")
	public DataMessage editExperience(
			@RequestParam(name="experienceId", required=false) String experienceId,
			@RequestParam(name="title", required=false) String title,
            @RequestParam(name="content", required=false) String content,
            @RequestParam(name="desc", required=false) String desc,
            @RequestParam(name="picture", required=false) String picture
			) {
		DataMessage dm = new DataMessage();
		// 数据验证自己写吧，只能帮你到这里了～
		this.experienceService.edit(Long.parseLong(experienceId), null, null, null, title, content, picture, desc, null, System.currentTimeMillis());
		dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		dm.setMessage("数据修改成功");
		return dm;
	}

   /**
	* 删除
	* @param experienceId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public DataMessage deleteExperience(@RequestParam(required=true, value="experienceId") Long experienceId) {
		DataMessage dm = new DataMessage();
		if(experienceId != null && experienceId > 0L) {
			this.experienceService.edit(experienceId, null, 2, null, null, null, null, null, null, null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setData(experienceId);
			dm.setMessage("数据删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	 /**
     * 批量删除政策
     * @param policyIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDelete(@RequestParam(required=true, value="experienceIds") String experienceIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(experienceIds, 1, 255)) {
            String[] experienceIdArray = experienceIds.split(",");
            for(String experienceId : experienceIdArray) {
            	this.experienceService.edit(Long.parseLong(experienceId), null, 2, null, null, null, null, null, null, null);
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游资讯删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}